/*
 * Copyright (c) 2018. QDU 2015 Java1 Team CompilationError
 */

/*
 * Copyright (c) 2018. QDU 2015 Java1 Team CompilationError
 */
$().ready(function () {

	$("#modify_user_form").validate({

		// debug:true,

		rules: {
			username: {
				required: true,
			},
			password: {
				rangelength: [6, 12],
			},
			cfm_password: {
				equalTo: "#password"
			},
			// gender: "required",
			departmentId: "required",
			designationId: "required",
			permissionGroupId: "required",
			mobile: {
				required: true,
				chineseMobileNumber: true
			},
			email: {
				required: true,
				email: true
			},
			salary: {
				required: true,
				number: true,
				range: [1, 1000000]
			}
		},

		messages: {
			username: {
				usernameCheck: "Username should start with a letter and only contains letters and digits",
				remote: "Username already exists"
			},
			mobile: "Invalid Mobile Number",
			cfm_password: "Password does not match"
		},

		submitHandler: function (form) {
			if ($("#password").val() != "") {
				$("#password").val(getEncodedPassword($("#password").val()));
			}
			$(form).ajaxSubmit({
				url: "modify_user",
				type: "post",
				dataType: "json",
				success: function (data) {
					if (data.status == "success") {
						showMessagePopup("Information", "User Successfully Updated", function () {
							window.top.close();
						});
					} else {
						showMessagePopup("Error", data.errorMessage);
					}
				},
				error: function (jqHXR, textStatus, error) {
					console.log(error);
				}
			});
		}
	});
});
